//
//  QuestionAreaViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/10.
//  Copyright © 2020 王洋. All rights reserved.
//


#import "QuestionAreaViewController.h"

@interface QuestionAreaViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSMutableArray <CommunityAreaModel *>*dataArray;


@end

@implementation QuestionAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_queryAll];
}

- (void)initView {
    
    self.title = @"提问领域";
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.dataArray.count;
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QuestionAreaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QuestionAreaCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    cell.mainTitleLab.text = self.dataArray[indexPath.row].fieldName;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.QuestionAreaBlock) {
        self.QuestionAreaBlock(self.dataArray[indexPath.row]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"QuestionAreaCell" bundle:nil] forCellReuseIdentifier:@"QuestionAreaCellID"];
    }
    return _tableview;
}

//API_QueryAll

- (void)request_queryAll {
        
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_QueryAll];
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在发布"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {

                NSArray *resultArr = [data valueForKey:@"data"];
                
                if ([resultArr isKindOfClass:[NSArray class]]) {
                    
                    weakSelf.dataArray = @[].mutableCopy;
                    for (NSDictionary *dic in resultArr) {
                        
                        CommunityAreaModel *model = [CommunityAreaModel modelWithDictionary:dic];
                        [weakSelf.dataArray addObject:model];
                    }
                    
                    [weakSelf.tableview reloadData];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
